##############################################################################################
## Stauffer Public Perceptions of Women's Inclusion and Feelings of Political Efficacy
## Replication Code - Figures from Appendix 12
############################################################################################

#load packages
library(ggplot2)
library(foreign)
library(readstata13)
library(gridExtra)

#load data
data<-read.dta13("../161718_CCES.dta")
head(data)
attach(data)
data$new<-(as.numeric(data$religiosity))
data$estimatesplot<-ifelse(data$r_estimate>50, 50, data$r_estimate)

#KNOWLEDGE
myvars <- c("know_fed", "estimatesplot")
newdata <- data[myvars]
knowb<-na.omit(newdata)
know<-subset(knowb, know_fed==1 | know_fed==5)

know.plot<-ggplot(know, aes(estimatesplot, color=as.factor(know_fed), fill=as.factor(know_fed)))+
  geom_density(size=1.5, alpha=.25)
know.plot<-know.plot+labs(x="\n Estimate % Women", y="Density \n",
                      title=" " )
know.plot<-know.plot+scale_fill_manual(values=c("white", "grey50" ), name="Pol. Knowledge", labels=c("Least", "Most"))
know.plot<-know.plot+scale_color_manual(values=c("black", "grey45") , name="Pol. Knowledge", labels=c("Least", "Most"))
know.plot<-know.plot+theme_bw()+scale_x_continuous(breaks=c(0, 10, 20, 30, 40, 51), labels=c("0", "10", "20", "30", "40", "50+"), limits=c(0, 50))+
  scale_y_continuous(breaks=c(0, .01, .02, .03, .04, .05,.06), labels=c("0", ".01", ".02", ".03", ".04", ".05", ".06"), limits=c(0, .065))
know.plot<-know.plot + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                         panel.background = element_blank(), axis.line = element_line(colour = "black"))
know.plot<-know.plot+theme(axis.text=element_text(size=16),
                           axis.title=element_text(size=16, face="bold"),
                           plot.title=element_text(size=16, face="bold"))
know.plot<-know.plot +
  theme(legend.justification=c(.05,.85), legend.position=c(.47,.9), legend.text=element_text(size=10), 
        legend.title=element_text(size=12))
know.plot

#EDUCATION
summary(data$coll)
myvars <- c("coll", "estimatesplot")
newdata <- data[myvars]
edb<-na.omit(newdata)
ed<-subset(edb, coll==0 | coll==1)

ed.plot<-ggplot(ed, aes(estimatesplot, color=as.factor(coll), fill=as.factor(coll)))+
  geom_density(adjust=1, size=1.5, alpha=.25)
ed.plot<-ed.plot+labs(x="\n Estimate % Women", y="Density \n",
                          title=" " )
ed.plot<-ed.plot+scale_fill_manual(values=c("white", "grey50" ), name="Education", labels=c("No College", "College"))
ed.plot<-ed.plot+scale_color_manual(values=c("black", "grey45") , name="Education", labels=c("No College", "College"))
ed.plot<-ed.plot+theme_bw()+scale_x_continuous(breaks=c(0, 10, 20, 30, 40, 50), labels=c("0", "10", "20", "30", "40", "50+"), limits=c(0, 50))+
  scale_y_continuous(breaks=c(0, .01, .02, .03, .04, .05, .06), labels=c("0", ".01", ".02", ".03", ".04", ".05", ".06"), limits=c(0, .065))
ed.plot<-ed.plot + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                             panel.background = element_blank(), axis.line = element_line(colour = "black"))
ed.plot<-ed.plot+theme(axis.text=element_text(size=16),
                       axis.title=element_text(size=16, face="bold"),
                       plot.title=element_text(size=16, face="bold"))
ed.plot<-ed.plot +
  theme(legend.justification=c(.05,.85), legend.position=c(.47,.9), legend.text=element_text(size=10), 
        legend.title=element_text(size=12))
ed.plot

#IDEOLOGY
myvars <- c("ideo7", "estimatesplot")
newdata <- data[myvars]
ideob<-na.omit(newdata)
ideo<-subset(ideob, ideo7==7 | ideo7==1)

ideo.plot<-ggplot(ideo, aes(estimatesplot, color=as.factor(ideo7), fill=as.factor(ideo7)))+
  geom_density(adjust=1, size=1.5, alpha=.25)
ideo.plot<-ideo.plot+labs(x="\n Estimate % Women", y=" Density \n",
                      title=" ")
ideo.plot<-ideo.plot+scale_fill_manual(values=c("white", "grey50" ), name="Ideology", labels=c("Liberal", "Conservative"))
ideo.plot<-ideo.plot+scale_color_manual(values=c("black", "grey45"), name="Ideology", labels=c("Liberal", "Conservative"))
ideo.plot<-ideo.plot+theme_bw()+scale_x_continuous(breaks=c(0, 10, 20, 30, 40, 50), labels=c("0", "10", "20", "30", "40", "50+"), limits=c(0, 50))+
  scale_y_continuous(breaks=c(0, .01, .02, .03, .04, .05, .06), labels=c("0", ".01", ".02", ".03", ".04", ".05", ".06"), limits=c(0, .065))
ideo.plot<-ideo.plot + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                         panel.background = element_blank(), axis.line = element_line(colour = "black"))
ideo.plot<-ideo.plot+theme(axis.text=element_text(size=16),
                           axis.title=element_text(size=16, face="bold"),
                           plot.title=element_text(size=16, face="bold"))
ideo.plot<-ideo.plot +
  theme(legend.justification=c(.05,.85), legend.position=c(.47,.9), legend.text=element_text(size=10), 
        legend.title=element_text(size=12))
ideo.plot

#AGE 
myvars <- c("age", "estimatesplot")
newdata <- data[myvars]
age<-na.omit(newdata)

age$gen<-NA
age$gen[age$age<=34]<-1
age$gen[age$age>=35 & age$age<60]<-2
age$gen[age$age>=60]<-3

age.plot<-ggplot(age, aes(estimatesplot, color=as.factor(gen), fill=as.factor(gen)))+
  geom_density(adjust=1, size=1.5, alpha=.25)
age.plot<-age.plot+labs(x="\n Estimate % Women", y="Density \n",
                              title=" ")
age.plot<-age.plot+scale_fill_manual(values=c("white", "grey50", "grey10"), labels=c("Under 35", "35-60", "60+"), name="Age Cohort")
age.plot<-age.plot+scale_color_manual(values=c("black", "grey45", "grey10"), labels=c("Under 35", "35-60", "60+"), name="Age Cohort")
age.plot<-age.plot+theme_bw()+scale_x_continuous(breaks=c(0, 10, 20, 30, 40, 50), labels=c("0", "10", "20", "30", "40", "50+"), limits=c(0, 50))+
  scale_y_continuous(breaks=c(0, .01, .02, .03, .04, .05, .06), labels=c("0", ".01", ".02", ".03", ".04", ".05", ".06"), limits=c(0, .065))
age.plot<-age.plot + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                                 panel.background = element_blank(), axis.line = element_line(colour = "black"))
age.plot<-age.plot+theme(axis.text=element_text(size=16),
                        axis.title=element_text(size=16, face="bold"),
                        plot.title=element_text(size=16, face="bold"))
age.plot<-age.plot +
  theme(legend.justification=c(.05,.85), legend.position=c(.5,.9), legend.text=element_text(size=10), 
        legend.title=element_text(size=12))
age.plot

#GENDER 
myvars <- c("gender_r", "estimatesplot")
newdata <- data[myvars]
gender<-na.omit(newdata)

gen.plot<-ggplot(gender, aes(estimatesplot, color=as.factor(gender_r), fill=as.factor(gender_r)))+
  geom_density(adjust=1, size=1.5, alpha=.15)
gen.plot<-gen.plot+labs(x="\n Estimate % Women", y="Density \n",
                        title=" " )
gen.plot<-gen.plot+scale_fill_manual(values=c("white", "grey50"), name="Sex", labels=c("Male", "Female"))
gen.plot<-gen.plot+scale_color_manual(values=c("black", "grey45"), name="Sex", labels=c("Male", "Female"))
gen.plot<-gen.plot+theme_bw()+scale_x_continuous(breaks=c(0, 10, 20, 30, 40, 50), labels=c("0", "10", "20", "30", "40", "50+"), limits=c(0, 50))+
  scale_y_continuous(breaks=c(0, .01, .02, .03, .04, .06), labels=c("0", ".01", ".02", ".03", ".04", ".05", ".06"), limits=c(0, .065))
gen.plot<-gen.plot + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                           panel.background = element_blank(), axis.line = element_line(colour = "black"))
gen.plot<-gen.plot+theme(axis.text=element_text(size=16),
                         axis.title=element_text(size=16, face="bold"),
                         plot.title=element_text(size=16, face="bold"))
gen.plot<-gen.plot +
  theme(legend.justification=c(.05,.85), legend.position=c(.5,.9), legend.text=element_text(size=10), 
        legend.title=element_text(size=12))
gen.plot

#RACE
myvars <- c("white", "estimatesplot")
newdata <- data[myvars]
race<-na.omit(newdata)

white.plot<-ggplot(race, aes(estimatesplot, color=as.factor(white), fill=as.factor(white)))+
  geom_density(adjust=1, size=1.5, alpha=.25)
white.plot<-white.plot+labs(x="\n Estimate % Women", y="Density \n",
                        title=" " )
white.plot<-white.plot+scale_fill_manual(values=c("white", "grey50"), name="Race", labels=c("Non-White", "White"))
white.plot<-white.plot+scale_color_manual(values=c("black", "grey45"), name="Race", labels=c("Non-White", "White"))
white.plot<-white.plot+theme_bw()+scale_x_continuous(breaks=c(0, 10, 20, 30, 40, 50), labels=c("0", "10", "20", "30", "40", "50+"), limits=c(0, 50))+
  scale_y_continuous(breaks=c(0, .01, .02, .03, .04, .05, .06), labels=c("0", ".01", ".02", ".03", ".04", ".05", ".06"), limits=c(0, .065))
white.plot<-white.plot + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                           panel.background = element_blank(), axis.line = element_line(colour = "black"))
white.plot<-white.plot+theme(axis.text=element_text(size=16),
                             axis.title=element_text(size=16, face="bold"),
                             plot.title=element_text(size=16, face="bold"))
white.plot<-white.plot +
  theme(legend.justification=c(.05,.85), legend.position=c(.5,.9), legend.text=element_text(size=10), 
        legend.title=element_text(size=12))
white.plot

#EMPLOYED 
myvars <- c("employed", "estimatesplot")
newdata <- data[myvars]
emp<-na.omit(newdata)

emp.plot<-ggplot(emp, aes(estimatesplot, color=as.factor(employed), fill=as.factor(employed)))+
  geom_density(adjust=1, size=1.5, alpha=.25)
emp.plot<-emp.plot+labs(x="\n Estimate % Women", y="Density \n",
                                title=" " )
emp.plot<-emp.plot+scale_fill_manual(values=c("white", "grey50"), name="Employment", labels=c("Unemployed", "Employed"))
emp.plot<-emp.plot+scale_color_manual(values=c("black", "grey45"), name="Employment", labels=c("Unemployed", "Employed"))
emp.plot<-emp.plot+theme_bw()+scale_x_continuous(breaks=c(0, 10, 20, 30, 40, 50), labels=c("0", "10", "20", "30", "40", "50+"), limits=c(0, 50))+
  scale_y_continuous(breaks=c(0, .01, .02, .03, .04, .05, .06), labels=c("0", ".01", ".02", ".03", ".04", ".05", ".06"), limits=c(0, .065))
emp.plot<-emp.plot + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                                   panel.background = element_blank(), axis.line = element_line(colour = "black"))
emp.plot<-emp.plot+theme(axis.text=element_text(size=16),
                         axis.title=element_text(size=16, face="bold"),
                         plot.title=element_text(size=16, face="bold"))
emp.plot<-emp.plot +
  theme(legend.justification=c(.05,.85), legend.position=c(.47,.9), legend.text=element_text(size=10), 
        legend.title=element_text(size=12))
emp.plot

#PARTISANSHIP 
test<-subset(data, party!=2)
myvars <- c("party", "estimatesplot")
newdata <- test[myvars]
test<-na.omit(newdata)

party2<-ggplot(test, aes(estimatesplot, color=as.factor(party), fill=as.factor(party)))+
  geom_density(adjust=1, size=1.5, alpha=.25)
party2<-party2+labs(x="\n Estimate % Women", y="Density \n",
                                title="" )
party2<-party2+scale_fill_manual(values=c("white", "grey50"), name="PID", labels=c("Democrat", "Republican"))
party2<-party2+scale_color_manual(values=c("black", "grey45"), name="PID", labels=c("Democrat", "Republican"))
party2<-party2+theme_bw()+scale_x_continuous(breaks=c(0, 10, 20, 30, 40, 50), labels=c("0", "10", "20", "30", "40", "50+"), limits=c(0, 50))+
  scale_y_continuous(breaks=c(0, .01, .02, .03, .04, .05, .06), labels=c("0", ".01", ".02", ".03", ".04", ".05", ".06"), limits=c(0, .065))
party2<-party2 + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                                   panel.background = element_blank(), axis.line = element_line(colour = "black"))
party2<-party2+theme(axis.text=element_text(size=16),
                     axis.title=element_text(size=16, face="bold"),
                     plot.title=element_text(size=16, face="bold"))
party2<-party2 +
  theme(legend.justification=c(.05,.85), legend.position=c(.47,.9), legend.text=element_text(size=10), 
        legend.title=element_text(size=12))
party2

#STRENGTH OF PARTISANSHIP
myvars <- c("strong_partisan", "estimatesplot")
newdata <- data[myvars]
strong<-na.omit(newdata)

strong<-ggplot(strong, aes(estimatesplot, color=as.factor(strong_partisan), fill=as.factor(strong_partisan)))+
  geom_density(adjust=1, size=1.5, alpha=.25)
strong<-strong+labs(x="\n Estimate % Women", y="Density \n",
                    title="" )
strong<-strong+scale_fill_manual(values=c("white", "grey50"), name="Strengh of PID", labels=c("Not Strong", "Strong"))
strong<-strong+scale_color_manual(values=c("black", "grey45"), name="Strengh of PID", labels=c("Not Strong", "Strong"))
strong<-strong+theme_bw()+scale_x_continuous(breaks=c(0, 10, 20, 30, 40, 50), labels=c("0", "10", "20", "30", "40", "50+"), limits=c(0, 50))+
  scale_y_continuous(breaks=c(0, .01, .02, .03, .04, .05, .06), labels=c("0", ".01", ".02", ".03", ".04", ".05", ".06"), limits=c(0, .065))
strong<-strong + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                       panel.background = element_blank(), axis.line = element_line(colour = "black"))
strong<-strong+theme(axis.text=element_text(size=16),
                     axis.title=element_text(size=16, face="bold"),
                     plot.title=element_text(size=16, face="bold"))
strong<-strong +
  theme(legend.justification=c(.05,.85), legend.position=c(.47,.9), legend.text=element_text(size=10), 
        legend.title=element_text(size=12))
strong

#RELIGIOSITY
myvars <- c("religiosity", "estimatesplot")
newdata <- data[myvars]
relig<-na.omit(newdata)
summary(relig$religiosity)
relig<-subset(relig, religiosity==1 | religiosity==6)

rel<-ggplot(relig, aes(estimatesplot, color=as.factor(religiosity), fill=as.factor(religiosity)))+
  geom_density(adjust=1, size=1.5, alpha=.25)
rel<-rel+labs(x="\n Estimate % Women", y="Density \n",
                    title="" )
rel<-rel+scale_fill_manual(values=c("white", "grey50"), name="Religiosity", labels=c("Least", "Most"))
rel<-rel+scale_color_manual(values=c("black", "grey45"), name="Religiosity", labels=c("Least", "Most"))
rel<-rel+theme_bw()+scale_x_continuous(breaks=c(0, 10, 20, 30, 40, 50), labels=c("0", "10", "20", "30", "40", "50+"), limits=c(0, 50))+
  scale_y_continuous(breaks=c(0, .01, .02, .03, .04, .05, .06), labels=c("0", ".01", ".02", ".03", ".04", ".05", ".06"), limits=c(0, .065))
rel<-rel + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                       panel.background = element_blank(), axis.line = element_line(colour = "black"))
rel<-rel+theme(axis.text=element_text(size=16),
               axis.title=element_text(size=16, face="bold"),
               plot.title=element_text(size=16, face="bold"))
rel<-rel +
  theme(legend.justification=c(.05,.85), legend.position=c(.5,.9), legend.text=element_text(size=10), 
        legend.title=element_text(size=12))
rel

#WOMAN SEN
myvars <- c("woman_sen", "estimatesplot")
newdata <- data[myvars]
sen<-na.omit(newdata)

senw<-ggplot(sen, aes(estimatesplot, color=as.factor(woman_sen), fill=as.factor(woman_sen)))+
  geom_density(adjust=1, size=1.5, alpha=.25)
senw<-senw+labs(x="\n Estimate % Women", y="Density \n",
              title="" )
senw<-senw+scale_fill_manual(values=c("white", "grey50"), name="Woman Senator", labels=c("No", "Yes"))
senw<-senw+scale_color_manual(values=c("black", "grey45"), name="Woman Senator", labels=c("No", "Yes"))
senw<-senw+theme_bw()+scale_x_continuous(breaks=c(0, 10, 20, 30, 40, 50), labels=c("0", "10", "20", "30", "40", "50+"), limits=c(0, 50))+
  scale_y_continuous(breaks=c(0, .01, .02, .03, .04, .05, .06), labels=c("0", ".01", ".02", ".03", ".04", ".05", ".06"), limits=c(0, .065))
senw<-senw + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                 panel.background = element_blank(), axis.line = element_line(colour = "black"))
senw<-senw+theme(axis.text=element_text(size=16),
                 axis.title=element_text(size=16, face="bold"),
                 plot.title=element_text(size=16, face="bold"))
senw<-senw +
  theme(legend.justification=c(.05,.85), legend.position=c(.6,.9), legend.text=element_text(size=10), 
        legend.title=element_text(size=12))
senw

#WOMAN REP
myvars <- c("woman_rep", "estimatesplot")
newdata <- data[myvars]
rep<-na.omit(newdata)

repw<-ggplot(rep, aes(estimatesplot, color=as.factor(woman_rep), fill=as.factor(woman_rep)))+
  geom_density(adjust=1, size=1.5, alpha=.25)
repw<-repw+labs(x="\n Estimate % Women", y="Density \n",
                title="" )
repw<-repw+scale_fill_manual(values=c("white", "grey50"), name="Woman Rep.", labels=c("No", "Yes"))
repw<-repw+scale_color_manual(values=c("black", "grey45"), name="Woman Rep.", labels=c("No", "Yes"))
repw<-repw+theme_bw()+scale_x_continuous(breaks=c(0, 10, 20, 30, 40, 50), labels=c("0", "10", "20", "30", "40", "50+"), limits=c(0, 50))+
  scale_y_continuous(breaks=c(0, .01, .02, .03, .04, .05, .06), labels=c("0", ".01", ".02", ".03", ".04", ".05", ".06"), limits=c(0, .065))
repw<-repw + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                   panel.background = element_blank(), axis.line = element_line(colour = "black"))
repw<-repw+theme(axis.text=element_text(size=16),
                 axis.title=element_text(size=16, face="bold"),
                 plot.title=element_text(size=16, face="bold"))
repw<-repw +
  theme(legend.justification=c(.05,.85), legend.position=c(.65,.9), legend.text=element_text(size=10), 
        legend.title=element_text(size=12))
repw

#WOMEN IN STATELEG
summary(data$stateleg)
state_data<-subset(data, stateleg<=20 | stateleg>=35)
myvars <- c("stateleg", "estimatesplot")
newdata <- state_data[myvars]
state_data<-na.omit(newdata)

state_data$group<-NA
state_data$group[state_data$stateleg<=20]<-"Under 20%"
state_data$group[state_data$stateleg>=35]<-"Over 35%"
summary(state_data$group)


state<-ggplot(state_data, aes(estimatesplot, color=as.factor(group), fill=as.factor(group)))+
  geom_density(adjust=1, size=1.5, alpha=.25)
state<-state+labs(x="\n Estimate % Women", y="Density \n",
                title="" )
state<-state+scale_fill_manual(values=c("white", "grey50"), name="Women in State Leg.")
state<-state+scale_color_manual(values=c("black", "grey45"), name="Women in State Leg.")
state<-state+theme_bw()+scale_x_continuous(breaks=c(0, 10, 20, 30, 40, 50), labels=c("0", "10", "20", "30", "40", "50+"), limits=c(0, 50))+
  scale_y_continuous(breaks=c(0, .01, .02, .03, .04, .05, .06), labels=c("0", ".01", ".02", ".03", ".04", ".05", ".06"), limits=c(0, .065))
state<-state + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                   panel.background = element_blank(), axis.line = element_line(colour = "black"))
state<-state+theme(axis.text=element_text(size=16),
                 axis.title=element_text(size=16, face="bold"),
                 plot.title=element_text(size=16, face="bold"))
state<-state +
  theme(legend.justification=c(.05,.85), legend.position=c(.5,.9), legend.text=element_text(size=10), 
        legend.title=element_text(size=12))
state

#Combine Plots 
grid.arrange(gen.plot, party2, ideo.plot, ed.plot, age.plot, know.plot, white.plot, emp.plot, strong, rel, ncol=5)
grid.arrange(senw, repw, state, ncol=3)

